package com.example.myapplication.acticity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

import com.example.myapplication.PlayService;
import com.example.myapplication.R;
import com.example.myapplication.adapter.MyMusicAdapter;
import com.example.myapplication.utils.MediaUtils;
import com.example.myapplication.vo.MobileApplication;
import com.example.myapplication.vo.Mp3Info;
import com.google.android.material.bottomsheet.BottomSheetDialog;

import java.util.ArrayList;

public class Mp3Activity extends BaseActivity {

    private TextView title,singer,startTime,endTime;
    private SeekBar seekBar;
    private ImageView play,push,last,next,cd,order,random,singm,sing_dlog;
    private ArrayList<Mp3Info> mp3Infos ;
    private ObjectAnimator animator;
    private static final int UPDATE_TIME = 0x1;
    private static MyHandler myHandler;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mp3);
        mp3Infos = MediaUtils.getMp3Infos(this);
        initView();
        myHandler = new MyHandler(this);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        Intent intent = getIntent();
        setResult(RESULT_OK,intent);
        finish();
    }

    /**
     * 设定进度条的更新和正在播放的时间的更新
     * @param progress
     */
    @Override
    public void publish(int progress) {
        Message message = myHandler.obtainMessage(UPDATE_TIME);
        message.arg1 = progress;
        myHandler.sendMessage(message);
        seekBar.setProgress(progress);
    }

    /**
     * 如果歌曲正在播放就填入歌曲信息
     * @param position
     */
    @Override
    public void change(int position) {

            Mp3Info mp3Info = mp3Infos.get(position);
            title.setText(mp3Info.getTitle());
            endTime.setText(MediaUtils.formatTime(mp3Info.getDuration()));
            singer.setText(mp3Info.getAlbum());
            seekBar.setProgress(0);
            seekBar.setMax((int) mp3Info.getDuration());


       onClick();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbindPlaySerevce();
    }

    /**
     * 按钮功能的实现
     */
    private void onClick() {

        if (MobileApplication.getInstance().playService.isPause()){
            push.setVisibility(View.GONE);
            play.setVisibility(View.VISIBLE);
        }else {
            play.setVisibility(View.GONE);
            push.setVisibility(View.VISIBLE);
            animator.start();
        }
        push.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                animator.pause();
                push.setVisibility(View.GONE);
                play.setVisibility(View.VISIBLE);
                MobileApplication.getInstance().playService.pause();
            }
        });

        play.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (MobileApplication.getInstance().playService.isPlaying()){
                    MobileApplication.getInstance().playService.start();
                }else {
                    MobileApplication.getInstance().playService.play(MobileApplication.getInstance().playService.getCurrentPostion());
                }
                animator.resume();
                play.setVisibility(View.GONE);
                push.setVisibility(View.VISIBLE);
            }
        });

        next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobileApplication.getInstance().playService.next();
                animator.start();
                play.setVisibility(View.GONE);
                push.setVisibility(View.VISIBLE);
            }
        });

        last.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                MobileApplication.getInstance().playService.prev();
                animator.start();
                play.setVisibility(View.GONE);
                push.setVisibility(View.VISIBLE);
            }
        });


        switch (MobileApplication.getInstance().playService.getPlay_modle()){
            case PlayService.ORDER_PLAY:
                singm.setVisibility(View.GONE);
                order.setVisibility(View.VISIBLE);
                random.setVisibility(View.GONE);
                break;
            case PlayService.RANDOM_PLAY:
                singm.setVisibility(View.GONE);
                random.setVisibility(View.VISIBLE);
                order.setVisibility(View.GONE);
                break;
            case PlayService.SINGLE_PLAY:
                random.setVisibility(View.GONE);
                singm.setVisibility(View.VISIBLE);
                order.setVisibility(View.GONE);
                break;
            default:
                break;

        }
        order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                order.setVisibility(View.GONE);
                random.setVisibility(View.VISIBLE);
                MobileApplication.getInstance().playService.setPlay_modle(PlayService.RANDOM_PLAY);
                Toast.makeText(Mp3Activity.this,"随机播放",Toast.LENGTH_SHORT).show();
            }
        });

        random.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                random.setVisibility(View.GONE);
                singm.setVisibility(View.VISIBLE);
                MobileApplication.getInstance().playService.setPlay_modle(PlayService.SINGLE_PLAY);
                Toast.makeText(Mp3Activity.this,"单曲循环",Toast.LENGTH_SHORT).show();
            }
        });
        singm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                singm.setVisibility(View.GONE);
                order.setVisibility(View.VISIBLE);
                MobileApplication.getInstance().playService.setPlay_modle(PlayService.ORDER_PLAY);
                Toast.makeText(Mp3Activity.this,"顺序播放",Toast.LENGTH_SHORT).show();
            }
        });

        sing_dlog.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                bottonSheet();
            }
        });

        seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int i, boolean b) {
                if (b){
                    MobileApplication.getInstance().playService.seekTo(i);
                }
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });

    }

    /**
     * 音乐列表的小窗
     */
    private void bottonSheet(){
        BottomSheetDialog sheetDialog = new BottomSheetDialog(Mp3Activity.this);
            View view = LayoutInflater.from(Mp3Activity.this).inflate(R.layout.dialog_botton,null,false);
            sheetDialog.setContentView(view);
            sheetDialog.getWindow().findViewById(R.id.dialog_recycleView).setBackgroundColor(Color.TRANSPARENT);
            RecyclerView recyclerView = view.findViewById(R.id.dialog_recycleView);
            recyclerView.setLayoutManager(new LinearLayoutManager(this));
            MyMusicAdapter adapter = new MyMusicAdapter(Mp3Activity.this,mp3Infos);
            adapter.setOnItemClickListener(new MyMusicAdapter.OnItemClickListener() {
                @Override
                public void OnItemClick(View view, int position) {
                    MobileApplication.getInstance().playService.play(position);
                }
            });
            recyclerView.setAdapter(adapter);
        sheetDialog.show();
    }

    private void initView() {

        title = findViewById(R.id.title);
        singer = findViewById(R.id.singer);
        startTime = findViewById(R.id.start_time);
        endTime = findViewById(R.id.end_time);
        seekBar = findViewById(R.id.seekBar2);
        play = findViewById(R.id.play);
        push = findViewById(R.id.push);
        last = findViewById(R.id.last);
        next = findViewById(R.id.next);
        cd = findViewById(R.id.cd);
        order = findViewById(R.id.order);
        random = findViewById(R.id.random);
        singm = findViewById(R.id.singm);
        sing_dlog = findViewById(R.id.sing_dlog);

        //对象是cd图片，动作是"rotation"旋转，角度从0到360，这里用的浮点数，所以加个f
        animator = ObjectAnimator.ofFloat(cd,"rotation",0,360.0f);
        animator.setDuration(10000);//设置动画的时长，这里用的毫秒，这里是10秒
        animator.setInterpolator(new LinearInterpolator());//设置时间函数为线性，就是匀速转动
        animator.setRepeatCount(-1);//设置转动圈数，-1就是一直旋转
    }

    static class MyHandler extends Handler{
        private Mp3Activity mp3Activity;

        public MyHandler(Mp3Activity mp3Activity){
            this.mp3Activity = mp3Activity;
        }
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            if (mp3Activity != null){
                switch (msg.what){
                    case UPDATE_TIME:
                        mp3Activity.startTime.setText(MediaUtils.formatTime((long) msg.arg1));
                        break;
                }
            }

        }
    }
}
